# 定义客户端行为，点击右边条目，可自动插入功能代码
# 定义客户端行为，点击右边条目，可自动插入功能代码

# 导入HAR文件中的http请求 
# 自己检查，去除不必要的请求

from urllib.parse import quote
from base64 import  b64encode
from random import randint
import json
import time

# 创建客户端     
client = HttpClient('127.0.0.1', # 目标地址:端口
                    timeout=10   # 超时时间，单位秒
                   ) 

response = client.sendAndRecv(
'GET',
  'http://127.0.0.1/distributor/sign.html',
  headers={
    'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'Accept-Encoding':'gzip, deflate, br',
    'Accept-Language':'zh-CN,zh;q=0.9',
    'Cache-Control':'max-age=0',
    'Connection':'keep-alive',
    'Host':'127.0.0.1',
    'If-Modified-Since':'Sat, 18 Apr 2020 13:08:52 GMT',
    'Sec-Fetch-Dest':'document',
    'Sec-Fetch-Mode':'navigate',
    'Sec-Fetch-Site':'none',
    'Sec-Fetch-User':'?1',
    'Upgrade-Insecure-Requests':'1',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
    'sec-ch-ua':'".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
    'sec-ch-ua-mobile':'?0',
    'sec-ch-ua-platform':'"Windows"'
  },

  
)


username = f'sell_{arg}'
password = '88888888'
up = b64encode(quote(username+'#$%'+password).encode())
    
response = client.post(
    '/api/distributor/signin',
    # 通过data传入指定urlencode格式的消息体参数
    data={'up':up})


# 获取消息头Content-Type值
jwt = response.headers['jwt']    
# print(f"消息头jwt值为 {jwt} ") 




# 请求 9
response = client.sendAndRecv(
'GET',
  'http://127.0.0.1/api/distributor/orders?action=list&pagenum=1&pagesize=5&keywords=&_=1660776285089',
 headers={
    'Accept':'*/*',
    'Accept-Encoding':'gzip, deflate, br',
    'Accept-Language':'zh-CN,zh;q=0.9',
    'Authorization':jwt,
    'Connection':'keep-alive',
    'Content-Type':'application/x-www-form-urlencoded',
    'Host':'127.0.0.1',
    'Referer':'http://127.0.0.1/distributor/index.html',
    'Sec-Fetch-Dest':'empty',
    'Sec-Fetch-Mode':'cors',
    'Sec-Fetch-Site':'same-origin',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
    'X-Requested-With':'XMLHttpRequest',
    'sec-ch-ua':'".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
    'sec-ch-ua-mobile':'?0',
    'sec-ch-ua-platform':'"Windows"'
  },

  
)

# 请求 11
response = client.sendAndRecv(
'GET',
  'http://127.0.0.1/favicon.ico',
  headers={
    'Accept':'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
    'Accept-Encoding':'gzip, deflate, br',
    'Accept-Language':'zh-CN,zh;q=0.9',
    'Connection':'keep-alive',
    'Host':'127.0.0.1',
    'Referer':'http://127.0.0.1/distributor/index.html',
    'Sec-Fetch-Dest':'image',
    'Sec-Fetch-Mode':'no-cors',
    'Sec-Fetch-Site':'same-origin',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
    'sec-ch-ua':'".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
    'sec-ch-ua-mobile':'?0',
    'sec-ch-ua-platform':'"Windows"'
  },

  
)


# 请求 12
response = client.sendAndRecv(
'GET',
  'http://127.0.0.1/api/distributor/users?action=list_customer&pagenum=1&pagesize=50&keywords=&fields=id%2Crealname&_=1660776285090',
  headers={
    'Accept':'*/*',
    'Accept-Encoding':'gzip, deflate, br',
    'Accept-Language':'zh-CN,zh;q=0.9',
    'Authorization':jwt,
    'Connection':'keep-alive',
    'Content-Type':'application/x-www-form-urlencoded',
    'Host':'127.0.0.1',
    'Referer':'http://127.0.0.1/distributor/index.html',
    'Sec-Fetch-Dest':'empty',
    'Sec-Fetch-Mode':'cors',
    'Sec-Fetch-Site':'same-origin',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
    'X-Requested-With':'XMLHttpRequest',
    'sec-ch-ua':'".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
    'sec-ch-ua-mobile':'?0',
    'sec-ch-ua-platform':'"Windows"'
  },

  
)


# 请求 13
response = client.sendAndRecv(
'GET',
  'http://127.0.0.1/api/distributor/medicines?action=list&pagenum=1&pagesize=50&keywords=&fields=id%2Cname&_=1660776285091',
  headers={
    'Accept':'*/*',
    'Accept-Encoding':'gzip, deflate, br',
    'Accept-Language':'zh-CN,zh;q=0.9',
    'Authorization':jwt,
    'Connection':'keep-alive',
    'Content-Type':'application/x-www-form-urlencoded',
    'Host':'127.0.0.1',
    'Referer':'http://127.0.0.1/distributor/index.html',
    'Sec-Fetch-Dest':'empty',
    'Sec-Fetch-Mode':'cors',
    'Sec-Fetch-Site':'same-origin',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
    'X-Requested-With':'XMLHttpRequest',
    'sec-ch-ua':'".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
    'sec-ch-ua-mobile':'?0',
    'sec-ch-ua-platform':'"Windows"'
  },

  
)

for i in range(10):
    customer_id = randint(4,3003)
    medicine_id = randint(2,1001)
    
    data = {"action":"wf_order",
            "wf_action":"wf_submit_order",
            "data":{"name":f"sell_{arg}_订单A{i+1}",
                    "desc":"三日内发货",
                    "customer_id":customer_id,
                    "medicinelist":[{"id":medicine_id,"v":"10","name":f"青霉素_{medicine_id-2}"}]
                    }
                }
    data=json.dumps(data)

                
    # 请求 14
    response = client.sendAndRecv(
    'POST',
      'http://127.0.0.1/api/distributor/orders',
      headers={
        'Accept':'*/*',
        'Accept-Encoding':'gzip, deflate, br',
        'Accept-Language':'zh-CN,zh;q=0.9',
        'Authorization':jwt,
        'Connection':'keep-alive',
        # 'Content-Length':'184',
        'Content-Type':'application/json',
        'Host':'127.0.0.1',
        'Origin':'http://127.0.0.1',
        'Referer':'http://127.0.0.1/distributor/index.html',
        'Sec-Fetch-Dest':'empty',
        'Sec-Fetch-Mode':'cors',
        'Sec-Fetch-Site':'same-origin',
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
        'X-Requested-With':'XMLHttpRequest',
        'sec-ch-ua':'".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
        'sec-ch-ua-mobile':'?0',
        'sec-ch-ua-platform':'"Windows"'
      },
    
      data=data
    )
    
    
    # 请求 15
    response = client.sendAndRecv(
    'GET',
      'http://127.0.0.1/api/distributor/orders?action=list&pagenum=1&pagesize=5&keywords=&_=1660776285092',
      headers={
        'Accept':'*/*',
        'Accept-Encoding':'gzip, deflate, br',
        'Accept-Language':'zh-CN,zh;q=0.9',
        'Authorization':jwt,
        'Connection':'keep-alive',
        'Content-Type':'application/x-www-form-urlencoded',
        'Host':'127.0.0.1',
        'Referer':'http://127.0.0.1/distributor/index.html',
        'Sec-Fetch-Dest':'empty',
        'Sec-Fetch-Mode':'cors',
        'Sec-Fetch-Site':'same-origin',
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
        'X-Requested-With':'XMLHttpRequest',
        'sec-ch-ua':'".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
        'sec-ch-ua-mobile':'?0',
        'sec-ch-ua-platform':'"Windows"'
      },
    
      
    )
    
    time.sleep(5)
  

print(f'sell_{arg} 的订单创建完成')

